﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ClassLibrary;
using System.Data.SqlClient;
using System.IO;

namespace WindowsFormsApplication1
{
    public partial class ImageTrasformSample : Form
    {
        public ImageTrasformSample()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {

            if (this.openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                Image img = new Bitmap(this.openFileDialog1.FileName);
                this.pictureBox1.Image = img;

                byte[] buffer =ClsImage.imgToBinary(img);

                string strConn = "Data Source=.; Initial Catalog=test; Integrated Security=True";
                using (SqlConnection conn = new SqlConnection(strConn))
                {
                    string strCmd = "INSERT INTO Images VALUES (@pic)";
                    using (SqlCommand cmd = new SqlCommand(strCmd, conn))
                    {
                        cmd.Parameters.AddWithValue("@pic", buffer);
                        conn.Open();
                        cmd.ExecuteNonQuery();
                        conn.Close();
                    }
                }
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            string strConn = "Data Source=.; Initial Catalog=test; Integrated Security=True";
            using (SqlConnection conn = new SqlConnection(strConn))
            {
                string strCmd = "SELECT TOP 1 * FROM Images";
                using (SqlCommand cmd = new SqlCommand(strCmd, conn))
                {
                    conn.Open();
                    object buffer = cmd.ExecuteScalar();            
                    
                    this.pictureBox1.Image = ClsImage.binaryToImage(((byte[])buffer));
                    conn.Close();
                }
            }
        }

        private void ImageTrasformSample_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'uBOXDataSet.unboxing_article' table. You can move, or remove it, as needed.            
        }

        private void button3_Click(object sender, EventArgs e)
        {
            OpenFileDialog fd = this.openFileDialog1;
            fd.Multiselect = true;

            if (fd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                string strConn = "Server=.; Database=UBOX; Trusted_Connection=True;";
                using (SqlConnection conn = new SqlConnection(strConn))
                {
                    string strCmd = "insert into unboxing_image (unboxing_article_id, image) values (@id, @image)";
                    using (SqlCommand cmd = new SqlCommand(strCmd, conn))
                    {
                        cmd.Parameters.AddWithValue("@id", SqlDbType.Int);
                        cmd.Parameters.AddWithValue("@image", SqlDbType.VarBinary);
                        
                        for (int i = 1; i <= 10; i++)
                        {
                            Image img = new Bitmap(fd.FileNames[i-1]);
                            byte[] buffer = ClassLibrary.ClsImage.imgToBinary(img);
                            cmd.Parameters["@id"].Value = i;
                            cmd.Parameters["@image"].Value = buffer;
                            conn.Open();
                            cmd.ExecuteNonQuery();
                            conn.Close();
                        }

                    }
                }
            }

        }
    }
}
